package com.zt.ztzx.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.zt.ztzx.model.dto.system.SysUserDto;
import com.zt.ztzx.model.entity.system.SysRole;
import com.zt.ztzx.model.entity.system.SysRoleUser;
import com.zt.ztzx.model.vo.common.Result;
import com.zt.ztzx.model.vo.system.LoginVo;
import com.zt.ztzx.model.dto.system.LoginDto;
import com.zt.ztzx.model.entity.system.SysUser;
import com.zt.ztzx.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 天山一剑卓不凡
 */
@RestController
@RequestMapping("admin/system/index")
@Tag(name = "首页统一接口", description = "首页接口")
//@CrossOrigin(origins = "*",originPatterns = "*", allowedHeaders = "*",allowCredentials = "true",)  //解决跨域的注解
public class IndexController {
    @GetMapping("index")
    //对具体接口的声明描述
    @Operation(summary = "首页接口方法", description = "首页接口11", method = "index方法")
    //参数声明注解
    @Parameters(value = {@Parameter(name = "id", description = "携带id参数", required = true)})
    public String index(@RequestParam("id") int id) {
        return "admin index";
    }

    @Autowired
    private SysUserService sysUserService;

    //登录接口
    @PostMapping("login")
    @Operation(summary = "登录接口方法", description = "登录接口", method = "login方法")
    @Parameters(value = {@Parameter(name = "loginDto", description = "登录接口请求参数", required = true)})
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        System.out.println(loginDto);
        return sysUserService.login(loginDto);
    }
    //获取用户信息,
    @GetMapping("userinfo")
    @Operation(summary = "获取用户信息接口方法", description = "获取用户信息接口", method = "info方法")
    //spirngmvc中如何获取请求头信息
    public Result<SysUser> info(@RequestHeader(name = "token")String token) {
        return sysUserService.getUserinfo(token);
    }
    //用户退出
    @GetMapping("logout")
    @Operation(summary = "用户退出接口方法", description = "用户退出接口", method = "logout方法")
    public Result logout(@RequestHeader(name = "token")String token) {
        sysUserService.logout(token);
        return Result.build(null, 200, "退出成功");
    }

    //获取用户

    @GetMapping("/list/{pageNum}/{pageSize}")
    @Operation(summary = "用户列表", description = "用户列表",method = "list方法")
    @Parameters({

            @Parameter(name = "sysUserDto",description = "用户查询条件",required = true),
            @Parameter(name = "pageNum",description = "当前页码",required = true),
            @Parameter(name = "pageSize",description = "每页显示条数",required = true)
    })
    public  Result<PageInfo<SysUser>> list(SysUserDto userDto, @PathVariable("pageNum")Integer pageNum, @PathVariable("pageSize")Integer pageSize){
        return sysUserService.list(userDto,pageNum,pageSize);
    }

    //添加用户
    @PostMapping("/remove")
    @Operation(summary = "添加用户", description = "添加用户",method = "save方法")
    @Parameters({
            @Parameter(name = "sysUser",description = "用户信息",required = true)
    })
    public Result add(@RequestBody SysUser sysUser){
        return sysUserService.saveUser(sysUser);
    }

    //根据用户ID查询用户角色
    @GetMapping("/selectSysUserAndRoleByUid/{userId}")
    @Operation(summary = "根据用户ID查询用户角色", description = "根据用户ID查询用户角色",method = "getRolesByUserId方法")
    @Parameters({
            @Parameter(name = "userId",description = "用户ID",required = true)
    })
    public Result<List<Integer>> getRolesByUserId(@PathVariable("userId")Integer userId){
        return sysUserService.getRolesByUserId(userId);
   }

    /**
     * 获取验证码
     * @return
     */
    @GetMapping(value = "/captcha",produces = MediaType.IMAGE_JPEG_VALUE)
    public Result captcha(HttpServletResponse response){
        return sysUserService.captcha(response);
    }
}
